package com.tuanzi.loan.web.bean;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.system.Announcement;
import com.tuanzi.loan.business.service.system.AnnouncementService;
import com.tuanzi.loan.web.configuration.JSFBean;
import com.tuanzi.loan.web.domain.MenuDomain;
import com.tuanzi.loan.workflow.service.WorkflowTaskService;
import lombok.Getter;
import lombok.Setter;
import org.primefaces.model.menu.MenuModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Ray 2017/09/21
 */
@Component
@SessionScoped
public class CurrentUserBean extends JSFBean {

	private static final long serialVersionUID = 1347560866756762306L;

	@Autowired
	private WorkflowTaskService workflowTaskService;
	@Autowired
	private AnnouncementService announcementService;

	private MenuDomain menuDomain;
	@Getter
	private MenuModel menuModel;
	@Getter
	private String name;

	@Setter
	private String back;

	@Getter
	private Long undoWork;
	@Getter
	private List<Announcement> announcements;
	@Getter
	private Boolean showAnnouncement = true;

	private static ConcurrentHashMap<String, Class> PAGE_2_LIST_MODEL_CLASS = new ConcurrentHashMap<>();
	private ConcurrentHashMap<Class, Object> modelClass2SearchParam;
	private ConcurrentHashMap<Class, Object> searchParamSession;

	@PostConstruct
	private void init() {
		menuDomain = new MenuDomain();
		menuModel = menuDomain.generateMenuModel(CurrentUser.get().getMenus());
		name = CurrentUser.get().getName();
		undoWork = workflowTaskService.countUnreadTodoTaskCount(CurrentUser.get().getAccount());
		loadAnnouncement();
		modelClass2SearchParam = new ConcurrentHashMap<>();
		searchParamSession = new ConcurrentHashMap<>();
	}

	public void refreshHeader() {
		undoWork = workflowTaskService.countUnreadTodoTaskCount(CurrentUser.get().getAccount());
		loadAnnouncement();
		if (showAnnouncement) {
			showAnnouncement = false;
			execute("PF('announcementDialogWidgetVar').show();");
		}
	}

	private void loadAnnouncement() {
		announcements = announcementService.findCurrentAnnouncement();
		if (announcements.size() <= 0) {
			showAnnouncement = false;
		}
	}

	/**
	 * 建立ListBean与对应页面的联系
	 * @param page
	 * @param listModelClass
	 */
	public static void addPageForListModel(String page, Class listModelClass) {
		PAGE_2_LIST_MODEL_CLASS.putIfAbsent(page, listModelClass);
	}

	/**
	 * 基于 {@link CurrentUserBean} 的作用域为 Session ，在 Session 中保存查询参数。
	 * @param searchParam
	 */
	public void addSearchParam(Object searchParam) {
		modelClass2SearchParam.put(searchParam.getClass(), searchParam);
	}

	public String getBack() {
		final String back = this.back;
		// 如果当前页面需要回显上一次的查询记录，拿出上一次保存的查询参数
		if (PAGE_2_LIST_MODEL_CLASS.containsKey(back)) {
			Class clazz = PAGE_2_LIST_MODEL_CLASS.get(back);
			Object searchParam = modelClass2SearchParam.remove(clazz);
			if (searchParam != null) {
				searchParamSession.put(clazz, searchParam);
			}
		}
		return back;
	}

	/**
	 * 返回上一个页面的查询参数
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public <T> T getSearchParamInSession(Class<T> clazz) {
		return (T) searchParamSession.remove(clazz);
	}
}
